import 'package:flutter/material.dart';
import 'dart:ui';

class PassWordLoginWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PassWordLoginWidgetState();
  }
}

class _PassWordLoginWidgetState extends State<PassWordLoginWidget> {
  static const String _hidePasswordIcon = 'assets/images/hide_pwd_pressed.png';
  static const String _showPasswordIcon = 'assets/images/hide_pwd_normal.png';

  TextEditingController _phoneNumberController;
  TextEditingController _passwordController;

  bool _showPassword = false;
  bool _enableLoginButton = false;

  @override
  void initState() {
    super.initState();
    _phoneNumberController = new TextEditingController();
    _passwordController = new TextEditingController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _phoneNumberController.dispose();
    _passwordController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding:
          EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0, bottom: 10.0),
      child: Column(
        children: <Widget>[
          _buildPhoneTextField(),
          Container(height: 16.0),
          _buildPasswordRow(),
          Container(height: 40.0),
          _buildLoginButton()
        ],
      ),
    );
  }

  Widget _buildPhoneTextField() => TextField(
        controller: _phoneNumberController,
        decoration: InputDecoration(hintText: '请输入手机号'),
        keyboardType: TextInputType.text,
      );

  Widget _buildPasswordTextField() => TextField(
        controller: _passwordController,
        decoration: InputDecoration(hintText: '请输入密码'),
        keyboardType: TextInputType.text,
        obscureText: _showPassword,
        onChanged: (String textChange) {
          setState(() {
            if (_phoneNumberController.text.toString().isEmpty ||
                textChange.isEmpty) {
              _enableLoginButton = false;
            } else {
              _enableLoginButton = true;
            }
          });
        },
      );

  Widget _buildPasswordVisibilityButton() => IconButton(
        icon: Image.asset(
          _showPassword ? _showPasswordIcon : _hidePasswordIcon,
          width: 20,
          height: 20,
        ),
        iconSize: 10.0,
        padding: EdgeInsets.all(0.0),
        onPressed: () {
          setState(() => _showPassword = !_showPassword);
        },
      );

  Widget _buildForgetPasswordButton() => FlatButton(
    padding: EdgeInsets.symmetric(horizontal: 0),
      color: Colors.white,
      child: Text('忘记密码',
          maxLines: 1,
          softWrap: false,
          style: TextStyle(color: Color.fromARGB(255, 0, 213, 142))),
      onPressed: () {

          double h = MediaQuery.of(context).size.height-MediaQueryData.fromWindow(window).padding.top;
          print(h);

      });

  Row _buildPasswordRow() => Row(
        children: <Widget>[
          Expanded(
            child: _buildPasswordTextField(),
            flex: 8,
          ),
          Expanded(child: _buildPasswordVisibilityButton(), flex: 1),
          Expanded(
              child: Container(
                color: Color.fromARGB(255, 0xdd, 0xdd, 0xdd),
                margin: EdgeInsets.only(
                    top: 2.0, bottom: 2.0, left: 10.0, right: 10),
                height: 30.0,
              ),
              flex: 1),
          Expanded(child: _buildForgetPasswordButton(), flex: 3)
        ],
      );

  Widget _buildLoginButton() {
    return RaisedButton(
      padding: EdgeInsets.symmetric(horizontal: 112.0),
      child: Text('登 录'),
      textColor: Colors.white,
      disabledColor: Colors.blueGrey,
      color: Colors.orange,
      onPressed: _enableLoginButton ? () {
      } : null,
    );
  }
}
